﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;


namespace AzureForm
{
    public partial class frmNewConnection : Form
    {
        public frmNewConnection()
        {
            InitializeComponent();
        }

        private void rdoExistingconn_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoExistingconn.Checked)
            {
                txtConnectionString.Enabled = true;
                rdoStorageCreds.Checked = false;
                rdoUseDev.Checked = false;
                rdoExistingconn.Checked = true;
                lblConnString.Enabled = true;
                lblAccountKey.Enabled = false;
                lblAccountname.Enabled = false;
                txtAccountKey.Enabled = false;
                txtAccountName.Enabled = false;
            }
        }

        private void rdoStorageCreds_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoStorageCreds.Checked)
            {
                lblConnString.Enabled = false;
                txtConnectionString.Enabled = false;
                rdoExistingconn.Checked = false;
                rdoUseDev.Checked = false;
                lblAccountKey.Enabled = true;
                lblAccountname.Enabled = true;
                txtAccountKey.Enabled = true;
                txtAccountName.Enabled = true;
                if (rdoUseCustom.Checked)
                {
                    txtConnectionString.Text = buildconnstring(txtAccountName.Text, txtAccountKey.Text, txtBlobEndpoint.Text, txtQueueEndpoint.Text, txtTableEndpoint.Text);
                }
                else
                {
                    txtConnectionString.Text = buildconnstring(txtAccountName.Text, txtAccountKey.Text, rdoUseHTTP.Checked);
                }
            }
        }

        private void rdoUseDev_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoUseDev.Checked)
            {
                lblConnString.Enabled = false;
                txtConnectionString.Enabled = false;
                rdoExistingconn.Checked = false;
                rdoStorageCreds.Checked = false;
                lblAccountKey.Enabled = false;
                lblAccountname.Enabled = false;
                txtAccountKey.Enabled = false;
                txtAccountName.Enabled = false;
                txtConnectionString.Text = buildconnstring(true);
            }
        }

        private void txtAccountName_TextChanged(object sender, EventArgs e)
        {
            if (rdoUseCustom.Checked)
            {
                txtConnectionString.Text = buildconnstring(txtAccountName.Text, txtAccountKey.Text, txtBlobEndpoint.Text, txtQueueEndpoint.Text, txtTableEndpoint.Text);
            }
            else
            {
                txtConnectionString.Text = buildconnstring(txtAccountName.Text, txtAccountKey.Text, rdoUseHTTP.Checked);
            }
        }

        private void rdoUseHTTP_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoUseHTTP.Checked)
            {
                rdoUseHTTPS.Checked = false;
                rdoUseCustom.Checked = false;
                txtQueueEndpoint.Enabled = false;
                txtBlobEndpoint.Enabled = false;
                txtTableEndpoint.Enabled = false;
                lblBlobendpoint.Enabled = false;
                lblQueueEndpoint.Enabled = false;
                lblTableEndpoint.Enabled = false;
                txtConnectionString.Text = buildconnstring(txtAccountName.Text, txtAccountKey.Text, rdoUseHTTP.Checked);
            }
        }

        private void rdoUseHTTPS_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoUseHTTPS.Checked)
            {
                rdoUseHTTP.Checked = false;
                rdoUseCustom.Checked = false;
                txtQueueEndpoint.Enabled = false;
                txtBlobEndpoint.Enabled = false;
                txtTableEndpoint.Enabled = false;
                lblBlobendpoint.Enabled = false;
                lblQueueEndpoint.Enabled = false;
                lblTableEndpoint.Enabled = false;
                txtConnectionString.Text = buildconnstring(txtAccountName.Text, txtAccountKey.Text, rdoUseHTTP.Checked);
            }
        }

        private void rdoUseCustom_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoUseCustom.Checked)
            {
                rdoUseHTTPS.Checked = false;
                rdoUseHTTP.Checked = false;
                txtQueueEndpoint.Enabled = true;
                txtBlobEndpoint.Enabled = true;
                txtTableEndpoint.Enabled = true;
                lblBlobendpoint.Enabled = true;
                lblQueueEndpoint.Enabled = true;
                lblTableEndpoint.Enabled = true;
                if (rdoUseCustom.Checked)
                {
                    txtConnectionString.Text = buildconnstring(txtAccountName.Text, txtAccountKey.Text, txtBlobEndpoint.Text, txtQueueEndpoint.Text, txtTableEndpoint.Text);
                }
                else
                {
                    txtConnectionString.Text = buildconnstring(txtAccountName.Text, txtAccountKey.Text, rdoUseHTTP.Checked);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
         

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                Configuration cfg = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
                ConnectionStringSettings css = new ConnectionStringSettings();
                css.ConnectionString = txtConnectionString.Text;

                if (txtAccountName.Text == string.Empty)
                {
                    css.Name = "Development Storage";
                }
                else
                {
                    css.Name = txtAccountName.Text;
                }

                cfg.ConnectionStrings.ConnectionStrings.Add(css);
                cfg.Save(ConfigurationSaveMode.Full);              
                MyMain.makenewmenuitem(css.Name, css.ConnectionString);
            }
            catch { }
            this.Close();
        }

        public MainQueueExplorer MyMain;

        private string buildconnstring(bool usedevstorage)
        {
            return "UseDevelopmentStorage=true";
        }

        private string buildconnstring(string AccountName, string AccountKey, bool usehttp)
        {
            string whichhttp="https";
            if (usehttp) {
                 whichhttp="http";
            }

            string connstr = string.Format("DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2}", whichhttp, AccountName.ToLower(), AccountKey);

            return connstr;
        }

        private string buildconnstring(string AccountName, string AccountKey, string BlobEndpoint, string QueueEndpoint, string TableEndpoint)
        {

            string connstr = string.Format("BlobEndpoint={0};QueueEndpoint={1};TableEndpoint={2};AccountName={3};AccountKey={4}", BlobEndpoint, QueueEndpoint, QueueEndpoint, AccountName.ToLower(), AccountKey);

            return connstr;
        }

        private void txtAccountKey_TextChanged(object sender, EventArgs e)
        {
            if (rdoUseCustom.Checked)
            {
                txtConnectionString.Text = buildconnstring(txtAccountName.Text, txtAccountKey.Text, txtBlobEndpoint.Text, txtQueueEndpoint.Text, txtTableEndpoint.Text);
            }
            else
            {
                txtConnectionString.Text = buildconnstring(txtAccountName.Text, txtAccountKey.Text, rdoUseHTTP.Checked);
            }
        }

        private void txtBlobEndpoint_TextChanged(object sender, EventArgs e)
        {
            txtConnectionString.Text = buildconnstring(txtAccountName.Text, txtAccountKey.Text, txtBlobEndpoint.Text, txtQueueEndpoint.Text, txtTableEndpoint.Text);
        }

        private void txtTableEndpoint_TextChanged(object sender, EventArgs e)
        {
            txtConnectionString.Text = buildconnstring(txtAccountName.Text, txtAccountKey.Text, txtBlobEndpoint.Text, txtQueueEndpoint.Text, txtTableEndpoint.Text);
        }

        private void txtQueueEndpoint_TextChanged(object sender, EventArgs e)
        {
            txtConnectionString.Text = buildconnstring(txtAccountName.Text, txtAccountKey.Text, txtBlobEndpoint.Text, txtQueueEndpoint.Text, txtTableEndpoint.Text);
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            btnClose.Enabled = false;
            btnTestConnection.Enabled = false;
            grpTesting.Visible = true;
            Application.DoEvents();
            CloudStorageAccount account;
            account = CloudStorageAccount.Parse(txtConnectionString.Text);

            Microsoft.WindowsAzure.StorageClient.CloudQueueClient ccq = account.CreateCloudQueueClient();
            Microsoft.WindowsAzure.StorageClient.CloudBlobClient ccb = account.CreateCloudBlobClient();
            Microsoft.WindowsAzure.StorageClient.CloudTableClient cct = account.CreateCloudTableClient();

            bool queueWorks = false,tableWorks=false,blobworks=false;

            chkBlob.CheckState = CheckState.Indeterminate;
            chkTable.CheckState = CheckState.Indeterminate;
            chkQueue.CheckState = CheckState.Indeterminate;
            Application.DoEvents();

            try
            {
                CloudBlob cb = ccb.GetBlobReference("myblob2871618");
                cb.DeleteIfExists();
                blobworks = true;
            }
            catch(StorageClientException)
            {
                blobworks = false;
            }

            chkBlob.CheckState = CheckState.Checked;
            chkBlob.Checked = blobworks;

            Application.DoEvents();
            try
            {
                cct.DoesTableExist("MyTable");
                tableWorks = true;
            }
            catch (StorageClientException)
            {
                tableWorks = false;
            }

            chkTable.CheckState = CheckState.Checked;
            chkTable.Checked = tableWorks;

            Application.DoEvents();
            try
            {
                CloudQueue cq = ccq.GetQueueReference("myqueue");
                cq.Exists();
                queueWorks = true;
            }
            catch (StorageClientException)
            {            
                queueWorks = false;
            }

            chkQueue.CheckState = CheckState.Checked;
            chkQueue.Checked = queueWorks;

            if (queueWorks && blobworks && tableWorks)
            {
                btnOK.Enabled = true;
            }
            else
            {
                btnOK.Enabled = false;
            }
   
            btnClose.Enabled = true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {            
            btnTestConnection.Enabled = true;
            grpTesting.Visible = false;
        }
    }
}
